<?php
/**
 * 富文本编辑器 Bos 图片上传
 *
 * Author: Zeonb
 * Date: 2018/1/27
 * Created by Panxsoft.
 */
namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\BosImageUpload\BosImageStoreRequest;
use App\Http\Requests\Admin\BosImageUpload\MultiBosImageStoreRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use App\Services\Utils\Bosable;
use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile;

class BosImageUploadController extends Controller
{
    use ApiChecker,
        ApiResponser,
        Bosable;

    const BOS_KEY_PREFIX = 'rich-text-images/';

    /**
     * 上传 Bos 图片
     *
     * @param BosImageStoreRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(BosImageStoreRequest $request)
    {
        $url = $this->uploadFileToUrl($request->image);

        return $this->response(compact('url'));
    }

    /**
     * 上传多张 Bos 图片
     *
     * @param MultiBosImageStoreRequest $request
     * @return \Illuminate\Http\Response
     */
    public function storeMulti(MultiBosImageStoreRequest $request)
    {
        $urls = collect($request->images)->map(function (UploadedFile $file) {
            return $this->uploadFileToUrl($file);
        });

        return $this->response(compact('urls'));
    }

    /**
     * @param UploadedFile $file
     * @return mixed
     */
    private function uploadFileToUrl(UploadedFile $file)
    {
        $bos_key = $this->uploadPostFile($file, self::BOS_KEY_PREFIX);

        $url = $this->getStaticUrl($bos_key);

        return $url;
    }


}
